<?php

/***************************************************************\
* FILE:		sys/inputcheck.inc
* SECTION:	core > input check
* FUNCTION:	offers functions for checking input
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

function check_input($bool, $str) {
	if (!$bool) {
		add_alert($str);
	}
	return $bool ? 1 : 0;
}

function check_exists($str, $fieldname="") {
	return check_input(strlen(trim($str)) > 0, "Geen invoer voor veld [$fieldname]");
}

function check_url($str, $fieldname="") {
	return check_input(ereg("^https?://", $str), "$fieldname begint niet met 'http://'");
}

function check_chars($text, $fieldname="") {
	return check_input((strlen(ereg_replace("[a-zA-Z0-9]", "", $text)) == 0), "Er zijn voor deze invoer enkel alfanumerieke tekens toegestaan [$fieldname]");
}

function check_int($text, $fieldname="") {
	return check_input((strlen(ereg_replace("[0-9]", "", $text)) == 0), "Er zijn voor deze invoer enkel alfanumerieke tekens toegestaan [$fieldname]");
}

function check_email($address, $fieldname="") {
	return check_input(ereg("^[-a-z0-9_\.]+@([-a-z0-9_]+\.)+[a-z]+$", strtolower($address)), "Ongeldig email-adres [$fieldname]");
}

function check_tags($text, $fieldname="") {
	return check_input((strip_tags($text) == $text), "Er is voor deze invoer geen HTML toegestaan [$fieldname]");
}

function check_tag($tag, $text, $fieldname="") {
	return check_input(!in_array($tag, get_tags($text)), "Tag '$tag' is niet toegestaan voor dit veld [$fieldname]");
}

function get_tags($text) {
	$tags = array();
	if (ereg("^<", $text))
		$firstistag = 1;
	$arr = explode("<", $text);
	array_shift($arr);
	while ($s = array_shift($arr)) {
		$s = ereg_replace("^ +","",$s);
		$k = strpos($s, ">");
		$l = strpos($s, " ");
		if ($k === false)
			continue;

		if ($l === false)
			$m = $k;
		else
			$m = min($k, $l);
		array_push($tags, strtolower(substr($s, 0, $m)));
	}
	return $tags;
}

function check_tag_closures($text, $checked_tags = 0) {
	$ts = get_tags($text);
	$ok = 1;
	if (!is_array($checked_tags))
		$checked_tags = array("table", "font", "blockquote", "center", "h1", "h2", "h3", "h4", "tr", "td", "ul", "li", "ol", "sup", "sub", "small", "quote", "dialect", "a", "b", "i", "u");
	$occurences = array();
	while ($t = @array_shift($ts)) {
		
		$closing = ereg("^/", $t) ? 1 : 0;

		$t = substr($t, $closing);

		if (in_array($t, $checked_tags)) {
			
			if (intval($occurences[$t]) == 0 and $closing) {
				$ok--;
				add_alert("Afsluitende &lt;{$t}> tag voor openende");
			}
			if ($closing)
				$occurences[$t] = intval($occurences[$t]) - 1;
			else
				$occurences[$t] = intval($occurences[$t]) + 1;
		
		}
	}
	if (is_array($occurences) and count($occurences)) {
		$used_tags = array_keys($occurences);
		while ($t = array_pop($used_tags))
			if ($occurences[$t] != 0) {
				$ok--;
				add_alert("Ongelijk aantal openende en sluitende &lt;{$t}> tags");
			}
	}
	return ($ok > 0);
}

function check_tags_markup($text, $fieldname="") {
	$ts = get_tags($text);
	$ok = check_tag_closures($text, array("b", "i", "u", "sub", "sup"));
	while ($t = @array_pop($ts))
		switch(strtolower(ereg_replace("^/","",$t))) {
			case "i":
			case "b":
			case "u":
			case "sub":
			case "sup":
				break;
			default:
				add_alert("tag '$t' wordt niet herkend of toegelaten voor deze invoer [$fieldname]");
				$ok = 0;
				break;
		}
	return $ok;
}

function check_tags_advanced($text, $fieldname="") {
	$ts = get_tags($text);
	$ok = check_tag_closures($text);
	while ($t = @array_pop($ts))
		switch(strtolower(ereg_replace("^/","",$t))) {
			case "i":
			case "b":
			case "u":
			case "sub":
			case "sup":
			case "img":
			case "a":
			case "table":
			case "td":
			case "tr":
			case "blockquote":
			case "quote":
			case "dialect":
			case "bullshit":
			case "font":
			case "ol":
			case "ul":
			case "li":
			case "small":
			case "h1":
			case "h2":
			case "h3":
			case "h4":
			case "center":
			case "p":
			case "pre":
			case "tt":
			case "br":
			case "br/":
			case "hr":
			case "hr/":
				break;
			default:
				add_alert("tag '$t' wordt niet herkend of toegelaten voor deze invoer [$fieldname]");
				$ok = 0;
				break;
		}
	return $ok;
}

function check_tags_tables($text, $fieldname="") {
	return check_tag_closures($text);
}

function check_date_valid($day, $month, $year, $fieldname="") {
	$ok = 1;
	if ($day <= 0 or $month <= 0 or $month > 12 or $year < 1900)
		$ok = 0;
	switch ($month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			if ($day > 31)
				$ok = 0;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			if ($day > 30)
				$ok = 0;
			break;
		case 2:
			if ($day > 29)
				$ok = 0;
			break;
		default:
			$ok = 0;
			break;
	}
	
	if ($ok)
		return 1;
		
	add_alert("Ongeldige datum voor invoer [$fieldname]");
	return 0;
}

function check_date_future($day, $month, $year, $fieldname="") {

	if (!check_date_valid($day, $month, $year, $fieldname))
		return 0;
	
	$ok = 1;	
	if ($year > date('Y'))
		return 1;
	else if ($year < date('Y'))
		$ok = 0;
	else
		if ($month > date('n'))
			return 1;
		else if ($month < date('n'))
			$ok = 0;
		else 
			if ($day > date('j'))
				return 1;
			else
				$ok = 0;
	
	if ($ok)
		return 1;
		
	add_alert("Datum '$day/$month/$year' valt in het verleden [$fieldname]");
	return 0;
}

function check_time($hours, $mins, $fieldname="") {
	if ($hours < 0 or $hours > 23 or $mins < 0 or $mins > 59) {
		add_alert("Tijdstip $hours:$mins is niet geldig als invoer [$fieldname]");
		return 0;
	}
	return 1;
}

?>